<?php
header("Content-Type:text/html;charset=utf-8");

include_once('helpers.php');

$path = isset($_GET['path']) ? trim($_GET['path']) : '';
$action = isset($_GET['action']) ?trim($_GET['action']) : '';
if(empty($path))
{
    $path = str_replace("\\", "/", dirname(__FILE__))."/demo";
}

$dirs = [];
$files = [];

$arr = scandir($path);

foreach($arr as $item)
{
    if($item == "." || $item == "..")
    {
        continue;
    }

    $current = $path.'/'.$item;

    if(is_dir($current))
    {
        $dirs[] = [
            'name' => $item,
            'type' => '文件夾',
            'size' => filesize($current),
            'time' => date("Y-m-d", filectime($current))
        ];

        
    }else
    {
        $files[] = [
            'name' => $item,
            'type' => pathinfo($current,PATHINFO_EXTENSION),
            'size' => filesize($current),
            'time' => date("Y-m-d", filectime($current))
        ];
    }
}

if($action == "newdir")
{
    $newdir = isset($_GET['newdir']) ? trim($_GET['newdir']) : '';

    if(empty($newdir))
    {
        notice('文件夹名称不能为空');
        exit;
    }

    $current = $path.'/'.$newdir;

    if(is_dir($current))
    {
        notice("文件夹已经存在了");
        exit;
    }

    $result = mkdir($current, 0777, true);

    if($result)
    {
        notice("文件夹已经创建成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("文件夹创建失败");
        exit;
    }
}

if($action == "newfile")
{
    $newfile = isset($_GET['newfile']) ? trim($_GET['newfile']) : '';

    if(empty($newfile))
    {
        notice('新的文件名称不能为空');
        exit;
    }

    $current = $path.'/'.$newfile;

    if(is_file($current))
    {
        notice('文件已存在');
        exit;
    }

    $result = @fopen($current, "w");

    if($result)
    {
        notice("文件创建成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("文件创建失败");
        exit;
    }
}

if($action == "editdir")
{
    $old = isset($_GET['old']) ? trim($_GET['old']) : '';
    $newdir = isset($_GET['newdir']) ? trim($_GET['newdir']) : '';

    if(empty($old))
    {
        notice("旧的文件夹名称不能为空");
        exit;
    }
    if(empty($newdir))
    {
        notice("新的文件夹名称不能为空");
        exit;
    }

    $old = $path.'/'.$old;
    $newdir = $path.'/'.$newdir;

    if(!is_dir($old))
    {
        notice("修改的旧的文件夹不存在");
        exit;
    }

    if(is_dir($newdir))
    {
        notice('新命名的文件夹名称已存在');
        exit;  
    }

    $result = rename($old, $newdir);

    if($result)
    {
        notice("重命名文件夹成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("重命名文件夹失败");
        exit;
    }
}

if($action == "deldir")
{
    $dir = isset($_GET['dir']) ? trim($_GET['dir']) : '';

    if(!is_dir($dir))
    {
        notice("要删除的文件夹不存在");
        exit;
    }

    if(@rmdir($dir))
    {
        notice("删除文件夹成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("删除文件夹失败");
        exit;
    }
}

if($action == "delfile")
{
    $file = isset($_GET['file']) ? trim($_GET['file']) : '';

    if(!is_file($file))
    {
        notice("要删除的文件不存在");
        exit;
    }

    if(@unlink($file))
    {
        notice("删除文件成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("删除文件失败");
        exit;
    }
}




if($action == "editfile")
{
    $old = isset($_GET['old']) ? trim($_GET['old']) : '';
    $newfile = isset($_GET['newfile']) ? trim($_GET['newfile']) : '';

    if(empty($old))
    {
        notice("旧的文件名称不能为空");
        exit;
    }

    if(empty($newfile))
    {
        notice("新的文件名称不能为空");
        exit;
    }

    $old = $path.'/'.$old;
    $newfile = $path.'/'.$newfile;

    if(!is_file($old))
    {
        notice("修改的旧的文件不存在");
        exit;
    }

    if(is_file($newfile))
    {
        notice('新命名的文件名称已存在');
        exit;
    }

    $result = rename($old, $newfile);

    if($result)
    {
        notice("重命名文件成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("重命名文件失败");
        exit;
    }
}

if($action == "delall")
{
    $dir = isset($_GET['dir']) ? trim($_GET['dir']) : '';

    if(empty($dir))
    {
        notice('文件夹路径为空');
        exit;
    }

    if(!is_dir($dir))
    {
        notice('文件夹不存在');
        exit;
    }

    $result = delete($dir);

    if($result)
    {
        notice("强制删除文件夹成功", "index.php?path=$path");
        exit;
    }else
    {
        noice("强制删除文件夹失败");
        exit;
    }
}

if($_POST)
{
    $result = upload('photo', $path);

    if($result)
    {
        notice("文件上传成功", "index.php?path=$path");
        exit;
    }else
    {
        notice("文件上传失败");
        exit;
    }
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件操作系统</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        table{
            width: 1000px;
            margin: 100px auto 0;
        }

        th{
            font-weight: bold;
        }

        th,td{
            height: 40px;
            line-height: 40px;
            font-size: 20px;
            text-align: center;
            transition: all linear .2s;
        }

        th:hover, td:hover{
            background: yellow;
        }
    </style>    
</head>
<body>
    <form method="post" enctype="multipart/form-data">
    <table align="center" border="1px" cellspacing="0px" cellpadding="0px">
        <tr>
            <th>文件名</th>
            <th>文件类型</th>
            <th>文件大小</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        
        <?php foreach($dirs as $item){?>
        <tr>
            <td><a href="index.php?path=<?php echo $path.'/'.$item['name'];?>"><?php echo $item['name'];?></a></td>
            <td><?php echo $item['type'];?></td>
            <td><?php echo $item['size'];?></td>
            <td><?php echo $item['time'];?></td>
            <td>
                <a href="javascript:void(0)" data-old="<?php echo $item['name'];?>" onclick="EditDir(this)">重命名</a>
                <a href="javascript:void(0)" data-dir="<?php echo $path.'/'.$item['name'];?>" onclick="DelDir(this)">刪除</a>
                <a href="javascript:void(0)" data-dir="<?php echo $path.'/'.$item['name'];?>" onclick="DelAll(this)">強制刪除</a>
            </td>
        </tr>        
        <?php }?>

        <?php foreach($files as $item){?>
        <tr>
            <td><?php echo $item['name'];?></td>
            <td><?php echo $item['type'];?></td>
            <td><?php echo $item['size'];?></td>
            <td><?php echo $item['time'];?></td>
            <td>
                <a href="javascript:void(0)" data-old="<?php echo $item['name'];?>" onclick="Editfile(this)">重命名</a>
                <a href="javascript:void(0)" data-file="<?php echo $path.'/'.$item['name'];?>" onclick="Delfile(this)">刪除</a>
            </td>
        </tr>        
        <?php }?>

        <tr>
            <td colspan="20">
                <a href="index.php">首页</a>
                <a href="javascript:history.go(1)">前进</a>
                <a href="javascript:history.go(-1)">后退</a>
                <a href="javascript:void(0)" id="AddDir">创建文件夾</a>
                <a href="javascript:void(0)" id="AddFile">创建文件</a>

                <input type="file" name="photo" id="photo" style="display:none" />
                <a href="javascript:void(0)" onclick="photo.click()">上传文件</a>
                <input type="hidden" name="action" value="upload" />
                <button type="submit">提交</button>
            </td>
        </tr>    
    </table>
    </form>            
</body>
</html>
<script>
    var path = "<?php echo $path; ?>"
    
    var AddDir = document.getElementById('AddDir');

    AddDir.onclick = function()
    {
        var newdir = prompt("请输入新的文件夹名称")

        if(newdir)
        {
            location.href = `index.php?path=${path}&newdir=${newdir}&action=newdir`
            return false
        }

    }
</script> 

<script>
    var path = "<?php echo $path;?>"

    var AddFile = document.getElementById('AddFile');

    AddFile.onclick = function()
    {
        var newfile = prompt("请输入新的文件名称")

        if(newfile)
        {
            location.href = `index.php?path=${path}&newfile=${newfile}&action=newfile`
            return false
        }
    }
</script>

<script>
    var path = "<?php echo $path;?>"

    function EditDir(self)
    {
        var old = self.dataset.old 

        var newdir = prompt("输入重命名文件夹名称")

        if(newdir)
        {
            location.href = `index.php?path=${path}&old=${old}&newdir=${newdir}&action=editdir`
            return false
        }
    }
</script>

<script>
    var path = "<?php echo $path;?>"

    function DelDir(self)
    {
        var dir = self.dataset.dir 

        if(confirm('是否确认删除文件夹'))
        {
            location.href = `index.php?path=${path}&dir=${dir}&action=deldir`
            return false
        }
    }
</script>  

<script>
    var path = "<?php echo $path;?>"

    function Delfile(self)
    {
        var file = self.dataset.file

        if(confirm('是否删除文件'))
        {
            location.href = `index.php?path=${path}&file=${file}&action=delfile`
            return false
        }
    }
</script>

<script>
    var path = "<?php echo $path;?>"

    function Editfile(self)
    {
        var old = self.dataset.old
        var newfile = prompt("输入重命名文件夹名称")

        if(newfile)
        {
            location.href = `index.php?path=${path}&old=${old}&newfile=${newfile}&action=editfile`
            return false
        }
    }
</script>

<script>
    var path = "<?php echo $path;?>"

    function DelAll(self)
    {
        var dir = self.dataset.dir 

        if(confirm("是否强制删除文件夹"))
        {
            location.href = `index.php?pth=${path}&dir=${dir}&action=delall`
            return false
        }
    }
</script>    